package com.wanxi.servlet;

import javafx.application.Application;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String method = req.getMethod();
//        System.out.println(method);
//        System.out.println(req.getContextPath());
//        System.out.println(req.getRequestURI());
//        System.out.println(req.getRequestURL());
//        System.out.println(req.getQueryString());
////        拿到所有请求头数据的key值,可以理解为Map<K,V>
//        Enumeration<String> headerNames = req.getHeaderNames();
//        while (headerNames.hasMoreElements()) {
//            String key = headerNames.nextElement();
//            System.out.println("key:" + key + " value:" + req.getHeader(key));
//        }
////        ServletInputStream inputStream = req.getInputStream();
//        BufferedReader reader = req.getReader();
//        String str = "";
//        while ((str = reader.readLine()) != null) {
//            System.out.println(str);
//        }
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
////        System.out.println(req.getServletPath());
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String remember = req.getParameter("remember");
        String code = req.getParameter("code");
        if (remember != null) {
            Cookie usernameCookie = new Cookie("username", username);
            resp.addCookie(usernameCookie);
            Cookie passwordCookie = new Cookie("password", password);
            resp.addCookie(passwordCookie);
        } else {
            Cookie[] cookies = req.getCookies();
            for (Cookie c : cookies
            ) {
                if (c.getName().equals("username") || c.getName().equals("password"))
                    c.setMaxAge(0);
            }
        }
        String sessionCode = (String) req.getSession().getAttribute("sCode");
        if (code.equals(sessionCode)) {
//            可以登录,登录的时候，应该还要查询用户名和密码是否存在?
            String user = "supeng";
            String pass = "aaaaa";
            if (user.equals(username) && pass.equals(password)) {
//                进入后台管理系统页面
//                怎么判断用户是否已经登录成功，需要将登录成功后，设置一个标识符
//                这个标识符应该在项目的整个周期都能拿到
//                所以将标识符保存到session或者application
                req.getSession().setAttribute("username", username);
                resp.sendRedirect("/manage.jsp");
            } else {
//                在密码错误页面点击返回按钮，返回到登录页面
                resp.sendRedirect("/error-password.jsp");
            }
        } else {
//            验证码不正确
            resp.sendRedirect("/error-code.jsp");
        }
//        req.getRequestDispatcher("login.jsp").forward(req, resp);

//        String[] aa = req.getParameterValues("hobby");
////        resp.getWriter().println(username);
//        PrintWriter out = resp.getWriter();
//        out.println("<html>");
//        out.println("<head>");
//        out.println("<meta charset=\"UTF-8\">");
//        out.println("<title>");
//        out.println("这是个测试");
//        out.println("</title>");
//        out.println("</head>");
//        out.println("<body>");
//        out.println("啊啊啊啊");
//        out.println("</body>");
//        out.println("</html>");

//        req.setAttribute("username", aa + "123");
//        req.getAttribute("username");
//        resp.setHeader("Accept-Encoding", "value");
//        req.getSession().setAttribute("username", aa + "123");
//        req.getServletContext().setAttribute("username", aa + "123");
//        resp.getWriter().println(aa);
//        resp.sendRedirect("index.jsp");
//        resp.setContentType("json/application&encode=UTF-8");
//        req.getRequestDispatcher("index.jsp").forward(req, resp);
//        if ("s".equals(aa)) {
////            页面转发:跳转页面之前需要转发数据到页面
//            req.getRequestDispatcher("success.html").forward(req, resp);
//        } else {
//            req.getRequestDispatcher("error.html").forward(req, resp);
//
//        }
    }
}
